package com.beesphere.excel.writer.style.resolvers;

import java.lang.reflect.Method;

import org.apache.poi.ss.usermodel.CellStyle;

import com.beesphere.excel.writer.impls.WorkbookContext;

public class StaticFieldBasedStyleResolver extends MethodBasedStyleResolver {
	
	private static final long serialVersionUID = 7829050146110274988L;
	
	private static final String EMPTY = "";
	
	protected String prefix = EMPTY;
	protected String suffix = EMPTY;

	public StaticFieldBasedStyleResolver (Method method, String prefix, String suffix) {
		super (method);
		this.prefix = prefix;
		if (this.prefix == null) {
			this.prefix = EMPTY;
		}
		this.suffix = suffix;
		if (this.suffix == null) {
			this.suffix = EMPTY;
		}
	}
	
	public StaticFieldBasedStyleResolver (Method method, String prefix) {
		this (method, prefix, null);
	}
	
	@Override
	protected Object getValue (WorkbookContext workbookContext, Object applyTo, Object value) {
		Class<?> clazz = null;
		if (applyTo == null) {
			applyTo = CellStyle.class;
		} else {
			clazz = applyTo.getClass();
		}
		try {
			return clazz.getField (prefix + ((String)value).toUpperCase () + suffix).get (null);
		} catch (Throwable e) {
			// IGNORE
		}
		return null;
	}

}
